package com.joycessh.action.cdgl;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.joycessh.VO.cdgl.VenueVO;
import com.joycessh.service.ICdglService;
import com.joycessh.service.impl.CdglServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public class UpdateVenueAction extends ActionSupport {
	
	private VenueVO venuevo;
	private ICdglService cdglService;	
	private String result;	
	private String message;
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	
	@Override
	public String execute() throws Exception {
		cdglService = new CdglServiceImpl();		
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String venueId = request.getParameter("venue_id");
			String  venueName= request.getParameter("venue_name");
			String  sport= request.getParameter("sport");
			String  str_openTime= request.getParameter("open_time");
			String  str_closeTime= request.getParameter("close_time");
			String  courtNum= request.getParameter("court_num");
			
			//System.out.println(str_openTime);
			//System.out.println(str_closeTime);
			//System.out.println(courtNum);
			
			//先把时间字符串格式化"hh:mm"，再转化为sql.Time
			SimpleDateFormat format = new SimpleDateFormat("hh:mm"); 
			Time openTime = new java.sql.Time(format.parse(str_openTime).getTime());
			Time closeTime = new java.sql.Time(format.parse(str_closeTime).getTime());
			
			//System.out.println(openTime);
			
			venuevo = new VenueVO();
			venuevo.setVenueId(venueId);
			venuevo.setVenueName(venueName);
			venuevo.setSport(sport);
			venuevo.setOpenTime(openTime);
			venuevo.setCloseTime(closeTime);
			venuevo.setCourtNum(courtNum);
			
			//调用添加场地服务，返回提示信息
			message = cdglService.updateCourt(venuevo);

			Map<String,Object> map = new HashMap<String,Object>();
			map.put("venue_id", venueId);
			map.put("venue_name", venueName);
			map.put("sport", sport);
			map.put("open_time", str_openTime);
			map.put("close_time", str_closeTime);
			map.put("court_num", courtNum);
			map.put("message", message);
			//将map对象转换成json类型数据*/
			JSONObject json = JSONObject.fromObject(map);
			//System.out.println(json);
			//给result赋值，传递给页面
			result = json.toString();
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
		
	}
}